package com.min.star.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.min.star.mapper.ExaminationMapper;
import com.min.star.model.Examination;
import com.min.star.model.Teacher;
import com.min.star.service.IExaminationService;

@Service
public class ExaminationServiceImpl implements IExaminationService {

	@Autowired
	private ExaminationMapper examinationMapper;

	@Override
	public List<Examination> selectExaminationList() {
		return this.examinationMapper.selectList(null);
	}

	@Override
	public Page<Examination> selectPage(Page<Examination> page,Examination examination) {
		return this.examinationMapper.selectPage(page, null);
	}
	
	@Override
	public Page<Examination> findPage(Page<Examination> page, Examination examination) {
		return examinationMapper.findPage(page, examination);
	}

	@Override
	public int delete(String ids) {
		return this.examinationMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
	}

	@Override
	public boolean checkExaminationExist4Add(@Valid Examination examination) {
		QueryWrapper<Examination> queryWrapper =new QueryWrapper<Examination>();
		queryWrapper.lambda().eq(Examination::getStuId,examination.getStuId());
		queryWrapper.lambda().eq(Examination::getCurId,examination.getCurId());
		int count = this.examinationMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}

	@Override
	public Examination save(Examination examination) {
		int ret =examinationMapper.insert(examination);
		Long id =examination.getId();
		Examination info =examinationMapper.findById(id);
		return info;
	}

	@Override
	public boolean checkExaminationExist4Edit(@Valid Examination examination) {
		QueryWrapper<Examination> queryWrapper =new QueryWrapper<Examination>();
		queryWrapper.lambda().eq(Examination::getStuId,examination.getStuId());
		queryWrapper.lambda().eq(Examination::getCurId,examination.getCurId());
		queryWrapper.lambda().ne(Examination::getId, examination.getId());
		int count = this.examinationMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}

	@Override
	public Examination updete(@Valid Examination examination) {
		int ret =examinationMapper.updateById(examination);
		return this.examinationMapper.findById(examination.getId());
	}
	
}
